package utils

import (
	"encoding/json"
	model2 "gobindc_test/server/model"
	"gobindc_test/server/tabulation"
	"net"
)

func HandleConnInRam(conn net.Conn, sqList *model2.SqList, dataConfig model2.DataConfig) {
	defer conn.Close()
	for {
		dataFromConn, err := getDataFromConn(conn)
		if err != nil {
			return
		}
		var data model2.SocketDataRequest
		err = json.Unmarshal([]byte(dataFromConn), &data)
		if err != nil {
			return
		}
		if data.Password == dataConfig.Password {
			switch data.Cmd {
			case "get":
				{
					ram, err := tabulation.GetDataFromTabulationInRam(sqList)
					if err != nil {
						response := model2.SocketDataResponse{
							Status: 401,
							Data:   err.Error(),
						}
						marshal, err := json.Marshal(response)
						if err != nil {
							return
						}
						conn.Write(marshal)
						break
					}
					marshal, err := json.Marshal(ram)
					if err != nil {
						return
					}
					response := model2.SocketDataResponse{
						Status: 200,
						Data:   string(marshal),
					}
					bytes, err := json.Marshal(response)
					if err != nil {
						return
					}
					conn.Write(bytes)
					break
				}
			case "set":
				{
					tabulation.InsertTabulationInRam(data.Data, sqList)
					response := model2.SocketDataResponse{
						Status: 200,
						Data:   "OK!",
					}
					marshal, err := json.Marshal(response)
					if err != nil {
						return
					}
					conn.Write(marshal)
					break
				}
			case "getAll":
				{
					break
				}
			}
		} else {
			dataResponse := model2.SocketDataResponse{
				Status: 403,
				Data:   "password is not success",
			}
			marshal, err := json.Marshal(dataResponse)
			if err != nil {
				return
			}
			conn.Write(marshal)
		}
	}
}

func HandleConnInDisk(conn net.Conn, sqList *model2.SqListDisk, dataConfig model2.DataConfig) {
	defer conn.Close()
	for {
		dataFromConn, err := getDataFromConn(conn)
		if err != nil {
			return
		}
		var data model2.SocketDataRequest
		err = json.Unmarshal([]byte(dataFromConn), &data)
		if err != nil {
			return
		}
		if data.Password == dataConfig.Password {
			switch data.Cmd {
			case "get":
				{
					ram, err := tabulation.GetDataFromTabulationInDisk(sqList)
					if err != nil {
						response := model2.SocketDataResponse{
							Status: 401,
							Data:   err.Error(),
						}
						marshal, err := json.Marshal(response)
						if err != nil {
							return
						}
						conn.Write(marshal)
						break
					}
					marshal, err := json.Marshal(ram)
					if err != nil {
						return
					}
					response := model2.SocketDataResponse{
						Status: 200,
						Data:   string(marshal),
					}
					bytes, err := json.Marshal(response)
					if err != nil {
						return
					}
					conn.Write(bytes)
					break
				}
			case "set":
				{
					tabulation.InsertTabulationInDisk(data.Data, sqList)
					response := model2.SocketDataResponse{
						Status: 200,
						Data:   "OK!",
					}
					marshal, err := json.Marshal(response)
					if err != nil {
						return
					}
					conn.Write(marshal)
					break
				}
			case "getAll":
				{
					break
				}
			}
		} else {
			dataResponse := model2.SocketDataResponse{
				Status: 403,
				Data:   "password is not success",
			}
			marshal, err := json.Marshal(dataResponse)
			if err != nil {
				return
			}
			conn.Write(marshal)
		}
	}
}

func getDataFromConn(conn net.Conn) (string, error) {
	buffer := make([]byte, 4096)
	n, err := conn.Read(buffer)
	s := string(buffer[:n])
	return s, err
}
